<?php

namespace App\Admin\Controllers;


use App\Models\Veidoo;
use App\Models\VeidooScore;

class CommonFcController{
    public static function StudentVeidooScore($stutdent_arr, $semester_id) {
        $result = [];
        $final_student_arr = [];
        $student_id_arr = [];
        $veidoo_arr = Veidoo::get();
        $weight_arr = [];
        $veidoo_type_arr = [];
        foreach ($veidoo_arr as $veidoo) {
            $weight_arr[$veidoo->id]['weight'] = $veidoo->weight / 10;
            $weight_arr[$veidoo->id]['name'] = $veidoo->name;
        }
        foreach ($stutdent_arr as $stutdent) {
            $stutdent_score_arr = VeidooScore::where(['semester_id'=>$semester_id, 'student_id' => $stutdent->student_id])->get();

            if(count($stutdent_score_arr) < count($weight_arr) * 3) {
                continue;
            }
            array_push($student_id_arr, $stutdent->student_id );
            $sutdent_detail = [];
//            dd($stutdent_score_arr);
            $student_weight_arr = [];

            foreach ($stutdent_score_arr as $stutdent_score) {

//                dd($stutdent_score);
                $veidoo_type_arr[$stutdent_score->veidoo_id][$stutdent_score->type] = $stutdent_score->score;

                if(!empty($student_weight_arr[$stutdent_score->veidoo_id]) ) {

                    switch ($stutdent_score->type) {
                        case 1:
                            $student_weight_arr[$stutdent_score->veidoo_id] += $stutdent_score->score * 0.4;
                            break;
                        case 2:
                            $student_weight_arr[$stutdent_score->veidoo_id] += $stutdent_score->score * 0.4;
                            break;
                        case 3:
                            $student_weight_arr[$stutdent_score->veidoo_id] += $stutdent_score->score * 0.2;
                            break;
                        default:
                            break;
                    }
                }else {

                    switch ($stutdent_score->type) {
                        case 1:
                            $student_weight_arr[$stutdent_score->veidoo_id] = $stutdent_score->score * 0.4;
                            break;
                        case 2:
                            $student_weight_arr[$stutdent_score->veidoo_id] = $stutdent_score->score * 0.4;
                            break;
                        case 3:
                            $student_weight_arr[$stutdent_score->veidoo_id] = $stutdent_score->score * 0.2;
                            break;
                        default:
                            break;
                    };
                }
            }
            $sutdent_detail['weight'] = $student_weight_arr;
            $final_score = 0;
            foreach ($student_weight_arr as $veidoo_id => $student_weight) {
                $final_score += $student_weight * $weight_arr[$veidoo_id]['weight'];
            }
            $sutdent_detail['final_score'] = $final_score;
            $final_student_arr[$stutdent->student_id] = $sutdent_detail;
        }


        $result['final_student_arr'] = $final_student_arr;
        $result['student_id_arr'] = $student_id_arr;
        $result['weight_arr'] = $weight_arr;
        $result['veidoo_type_arr'] = $veidoo_type_arr;
        return $result;
    }
}
